package com.android.tiango.baba.act;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.SocialUtils;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.utils.OauthHelper;

public class ActSocial extends BaBaseActivity implements OnClickListener {
	private Button btn_shareWX, btn_shareWXCircle, btn_shareQQ, btn_shareQQZoe,
			btn_shareSina, btn_shareSms,btn_loginQQ,btn_loginSina;
	private SocialUtils utils;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.layout_openshare);
		btn_shareWX = (Button) findViewById(R.id.btn_shareWX);
		btn_shareWXCircle = (Button) findViewById(R.id.btn_shareWXCircle);
		btn_shareQQ = (Button) findViewById(R.id.btn_shareQQ);
		btn_shareQQZoe = (Button) findViewById(R.id.btn_shareQQZoe);
		btn_shareSina = (Button) findViewById(R.id.btn_shareSina);
		btn_shareSms = (Button) findViewById(R.id.btn_shareSms);
		btn_loginQQ = (Button) findViewById(R.id.btn_loginQQ);
		btn_loginSina = (Button) findViewById(R.id.btn_loginSina);
		
		btn_loginQQ.setOnClickListener(this);
		btn_loginSina.setOnClickListener(this);

		btn_shareWX.setOnClickListener(this);
		btn_shareWXCircle.setOnClickListener(this);
		btn_shareQQ.setOnClickListener(this);
		btn_shareQQZoe.setOnClickListener(this);
		btn_shareSina.setOnClickListener(this);
		btn_shareSms.setOnClickListener(this);

		utils = new SocialUtils();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    /**使用SSO授权必须添加如下代码 */  
	    UMSsoHandler ssoHandler = SocialUtils.mController.getConfig().getSsoHandler(requestCode);
	    if(ssoHandler != null){
	       ssoHandler.authorizeCallBack(requestCode, resultCode, data);
	    }
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_shareWX:
			utils.setShareWeiXin(ActSocial.this);
			break;
		case R.id.btn_shareWXCircle:
			utils.setShareWeiXin_Circle(ActSocial.this, "朋友圈内容", "http://www.baidu.com", "朋友圈标题");
			break;
		case R.id.btn_shareQQ:
			utils.setShareQQ(ActSocial.this);
			break;
		case R.id.btn_shareQQZoe:
			utils.setShareQQZoe(ActSocial.this,"空间内容","http://www.baidu.com","空间标题");
			break;
		case R.id.btn_shareSina:
			utils.setShareSina(ActSocial.this,"新浪微博","http://www.baidu.com","浪标题");
			break;
		case R.id.btn_shareSms:
			utils.setShareSms(ActSocial.this, "test");
			break;
		case R.id.btn_loginQQ:
			if(!OauthHelper.isAuthenticated(ActSocial.this,SHARE_MEDIA.QQ)){
				login_Third(SHARE_MEDIA.QQ);
			}else{
				Toast.makeText(ActSocial.this, "QQ已经授权.", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btn_loginSina:
			boolean b = OauthHelper.isAuthenticated(ActSocial.this,SHARE_MEDIA.SINA);
			if(!b){
				login_Third(SHARE_MEDIA.SINA);
			}else{
				Toast.makeText(ActSocial.this, "新浪平台已经授权.", Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
	}

	private void login_Third(SHARE_MEDIA media) {
		UMSocialService mController = UMServiceFactory.getUMSocialService(Constants.UMENG_SHARE_LOGIN);
		
		mController.doOauthVerify(ActSocial.this, media,
				new UMAuthListener() {
					@Override
					public void onError(SocializeException e,
							SHARE_MEDIA platform) {
					}

					@Override
					public void onComplete(Bundle value,
							SHARE_MEDIA platform) {
						if (value != null
								&& !TextUtils.isEmpty(value
										.getString("uid"))) {
							Toast.makeText(ActSocial.this, "授权成功.",
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(ActSocial.this, "授权失败",
									Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onCancel(SHARE_MEDIA platform) {
					}

					@Override
					public void onStart(SHARE_MEDIA platform) {
					}
				});
	}
}
